package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealCheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;
    @Autowired
    private SetmealCheckGroupMapper setmealCheckGroupMapper;


    /**
     * 新增检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CheckGroup checkGroup, Integer[] checkitemIds) {
        // 新增检查组项
        try{
            Integer num = checkGroupMapper.insert(checkGroup);
            Integer groupId = checkGroup.getId();
            log.info("新增检查组成功, id为{}", groupId);

            if (checkitemIds != null && checkitemIds.length != 0) {
                // 直接一次性插入
                checkGroupCheckItemMapper.insert(groupId, checkitemIds);
                log.info("新增检查组项关联表成功, id为{}", groupId);
            }
            return num > 0;
        }catch (Exception e){
            log.info("新增检查组失败{}", e.getMessage());
            return false;
        }
    }

    /**
     * 删除检查组
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        // 如果和菜单关联则不能删除
        Integer hasNumInRelation = setmealCheckGroupMapper.hasCheckGroupId(id);
        if (hasNumInRelation > 0) {
            log.info("删除检查组失败，检查组被套餐关联");
            return false;
        }
        // 先删除检查组项关联表
        checkGroupCheckItemMapper.deleteByGroupId(id);
        // 删除检查组
        checkGroupMapper.deleteById(id);
        return true;
    }

    /**
     * 编辑检查组
     *
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(CheckGroup checkGroup, Integer[] checkitemIds) {

        Integer groupId = checkGroup.getId();
        // 判断检查组id是否为空，防止恶意攻击
        if (groupId == null) {
            log.info("编辑检查组失败，检查组id为空");
            return false;
        }
        // 判断检查组是否存在，防止恶意攻击
        CheckGroup groupOld = checkGroupMapper.selectById(groupId);
        if (groupOld == null) {
            log.info("编辑检查组失败，检查组不存在");
            return false;
        }
        // 根据id先删除检查组项关联表， 再重新进行插入
        checkGroupCheckItemMapper.deleteByGroupId(groupId);
        if (checkitemIds != null && checkitemIds.length > 0){
            checkGroupCheckItemMapper.insert(groupId, checkitemIds);
        }

        // 编辑检查组
        checkGroupMapper.update(checkGroup);
        return true;
    }

    /**
     * 分页查询
     * @param queryString
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageResult findPage(String queryString, Integer currentPage, Integer pageSize) {
        if(queryString == null) queryString = "";
        if(currentPage == null) currentPage = 1;
        if(pageSize == null) pageSize = 10;

        PageHelper.startPage(currentPage, pageSize);
        List<CheckGroup> checkGroup = checkGroupMapper.search(queryString);
        Page<CheckGroup> page = (Page<CheckGroup>) checkGroup;
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据检查组id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.selectById(id);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.search("");
    }

    /**
     * 根据套餐id查询检查组id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        log.info("根据套餐id查询检查组id, id为{}", id);
        return setmealCheckGroupMapper.selectBySetmealId(id);
    }
}
